import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'
import type { RouteRecordRaw } from 'vue-router'

// 静态路由
export const staticRoutes = <RouteRecordRaw[]>[
  {
    path: '/',
    name: 'layout',
    redirect: '/home',
    component: Layout,
    children: [
      {
        path: '/home',
        name: '首页',
        icon: 'House',
        component: () => import('@/views/home/index.vue')
      },
      {
        path: '/403',
        name: '403',
        component: () => import('@/views/errorPages/403.vue'),
        meta: { hidden: true }
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
    meta: { hidden: true }
  },
]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  //使用浏览器的回退或者前进时，重新返回时保留页面滚动位置，跳转页面的话，不触发。
  scrollBehavior(to, from, savePosition) {
    if (savePosition) {
      return savePosition
    } else {
      return { top: 0 }
    }
  },
  routes: staticRoutes
})

export const notFindRoute = {
  path: '/:pathMatch(.*)*',
  name: '404',
  component: () => import('@/views/errorPages/404.vue'),
  meta: { hidden: true }
}

export default router
